var proof = require('../../utils/proof.js');
var constract = require('../../utils/constract.js');
var app = getApp();
var myCardId, num, oldCompany, newCompany, uploadAva;
Page({
  data: {
    flag:false,
    dirty:false,
  },
  onLoad: function (options) {
    var that=this;
    console.log(options);
    
    myCardId = options.mycardid;

    app.Verification(function (token) {
      wx.request({
        url: constract.showMyCard,
        header: { token: token },
        data: {cardId: myCardId},
        success: function (res) {
          var userInfo = res.data.data;
          
          console.log(res);
          console.log(userInfo);
          
          num = userInfo ? userInfo.introduction.length : 0

          that.setData({
            userInfo: userInfo,
            company_name: userInfo.company_name,
            avatar: userInfo.avatar,
            phone: userInfo.phone,
            num: num,
            introduction: userInfo.introduction,
          });

        }
      });
    })
    wx.getSystemInfo({
      success: function (res) {
        var abc = res.system.substr(0, 3);
        if (abc == "iOS") {
          that.setData({
            flag:true
          })
        }
      },
    });
    
  },
  onShow: function(){
    var that = this;
    // dirty=false;
    // newCompany = '';
    app.Verification(function (token) {
      wx.request({
        url: constract.showMyCard,
        header: { token: token },
        data: {cardId: myCardId},
        success: function (res) {
          var userInfo = res.data.data;
          console.log(userInfo);
          that.setData({
            phone: userInfo.phone,
          })
        }
      });
    })
  },
  onReady: () => {
    //获取窗口高度
    wx.getSystemInfo({
      success: (res) => {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc);
      }
    })
  },
  avatar: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      
      success: function (res) {
        var tempFilePaths = res.tempFilePaths[0];
        uploadAva = true;
        
        that.setData({
          avatar: tempFilePaths,
        })
      },
      fail: function(err){
      }
    })
  },
  bindinput:function(e){
    var that = this;
    num=e.detail.value.length;
    setTimeout(function(){
      that.setData({
        num:num
      })
    },100)
    
  },
  modify: function(){
    wx.navigateTo({
      url: 'edit-phone/edit-phone?cardId='+this.data.userInfo.id,
    })
  },
  focusCompany: function(e){
    oldCompany = e.detail.value;
  },
  blurCompany:function(e){
    newCompany = e.detail.value;

    var dirty = (newCompany === this.data.company_name) ? false : true;
    this.setData({
      // userInfo: userInfo,
      // company_name: e.detail.value,
      dirty: dirty,
    })
  },
  submit:function(e){
    var that = this;
    var data = e.detail.value;
    var name = e.detail.value.name;
    var email = e.detail.value.email;
    var avatar = this.data.avatar;
    var company = e.detail.value.company_name;
    var dirty = this.data.dirty;

    data.cardId = myCardId;

    if (proof.trim(name)==''){
      wx.showModal({
        title: '提示',
        content: '姓名不能为空',
        showCancel: false
      });
      return;
    };
    if (proof.trim(company) == '') {
      wx.showModal({
        title: '提示',
        content: '公司不能为空',
        showCancel: false
      });
      return;
    };
    if (proof.trim(email)!==''){
      if (!proof.isEmail(email)){
        wx.showModal({
          title: '提示',
          content: '请输入正确的邮箱格式',
          showCancel: false
        });
        return;
      }
    };
    if (wx.showLoading) {
      wx.showLoading({
        mask: true,
        title: '保存中',
      });
    }

    console.log('-----------');
    console.log(company);

    app.Verification(function (token) {

      if(dirty) {
        wx.request({
          url: constract.isExist,
          header:{token:token},
          data: {company_name: company},
          success: (res)=>{
            console.log('--------');
            console.log(res);
  
            if(+res.data.code === 13000){ //dirty 公司名称改变 Boolean
              if (wx.hideLoading) {
                wx.hideLoading();
              };
              wx.showModal({
                title: '提示',
                content: '该名片信息已存在',
                success: function(res) {
                  if (res.confirm) {
                    // wx.redirectTo({
                    //   url:'/pages/edit/edit?mycardid='+cardId,
                    // })
                  } else if (res.cancel) {
                    console.log('用户点击取消');
                    return false;
                  }
                }
              })
            } else {
              wx.request({
                url: constract.userEdit,
                header:{token:token},
                data:data,
                success:function(res){
                  console.log(res);
                  if(uploadAva){
                    wx.uploadFile({
                      url: constract.imgsUpdate,
                      header: { token: token },
                      formData: {cardId: myCardId},
                      filePath: avatar,
                      name: "avatar",
                      complete: function (res) {
                        if (wx.hideLoading) {
                          wx.hideLoading();
                        };
                        wx.navigateBack();
                      }
          
                    })
                  }
                  if (wx.hideLoading) {
                    wx.hideLoading();
                  };
                  setTimeout(()=>{wx.navigateBack();},1000);
                },
                fail: function(err){
                  console.log(err);
                },
                complete: function (res) {
                  if (wx.hideLoading) {
                    wx.hideLoading();
                  };
                  // wx.navigateBack();
                }
              })
            }
          }
        })
      } else {
        wx.request({
          url: constract.userEdit,
          header:{token:token},
          data:data,
          success:function(res){
            console.log(res);
            if(uploadAva){
              wx.uploadFile({
                url: constract.imgsUpdate,
                header: { token: token },
                formData: {cardId: myCardId},
                filePath: avatar,
                name: "avatar",
                complete: function (res) {
                  if (wx.hideLoading) {
                    wx.hideLoading();
                  };
                  wx.navigateBack();
                }
    
              })
            }
            if (wx.hideLoading) {
              wx.hideLoading();
            };

            setTimeout(()=>{wx.navigateBack();},1000);
          },
          fail: function(err){
            console.log(err);
          },
          complete: function (res) {
            if (wx.hideLoading) {
              wx.hideLoading();
            };
            // wx.navigateBack();
          }
        })
      }
    })
  }
})